package com.didi.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.didi.gulimall.common.constant.BizCodeEnume;
import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.Query;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.coupon.dao.SeckillSessionDao;
import com.didi.gulimall.coupon.entity.SeckillSessionEntity;
import com.didi.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.didi.gulimall.coupon.service.SeckillSessionService;
import com.didi.gulimall.coupon.service.SeckillSkuRelationService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {
    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public R getInfoNearlyThreeDays() {
        QueryWrapper<SeckillSessionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("start_time", startTime(), endTime());
        List<SeckillSessionEntity> seckillPromotions = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(seckillPromotions)) {
            seckillPromotions = seckillPromotions.stream().peek(item -> {
                List<SeckillSkuRelationEntity> promotionSessions = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", item.getId()));
                item.setRelationSkus(promotionSessions);
            }).collect(Collectors.toList());
            return R.ok().put("data", seckillPromotions);
        }
        return R.error(BizCodeEnume.NO_SECKILL_PRODUCTS.getMsg());
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(2);
        LocalDateTime of = LocalDateTime.of(localDate, LocalTime.MAX);
        return of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalDateTime of = LocalDateTime.of(now, LocalTime.MIN);
       return of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    }
}